package com.ck.ips.service.impl;

import com.ck.common.SessionUser;
import com.ck.common.model.OperationResult;
import com.ck.common.model.User;
import com.ck.core.constant.SymbolConstant;
import com.ck.core.context.UserContextHolder;
import com.ck.ips.dao.DeviceGroupMapper;
import com.ck.ips.dao.UserInfoMapper;
import com.ck.ips.dao.store.DepartmentStoreMapper;
import com.ck.ips.dao.sys.UserMapper;
import com.ck.ips.model.vo.UserInfoVO;
import com.ck.ips.service.UserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 用户个人信息serviceImpl
* @Author: hf
* @Date:    2019/5/6 17:35
* @Version: 1.0
*/
@Service(value="userInfoService")
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DepartmentStoreMapper departmentStoreMapper;
    @Autowired
    private DeviceGroupMapper deviceGroupMapper;

    @Override
    public UserInfoVO getUserInfo() {
        return userInfoMapper.getUserInfoByUserName(UserContextHolder.currentUser().getUserName());
    }

    @Override
    @Transactional( rollbackFor = Exception.class )
    public OperationResult updateUserInfo(String avatar, String nickName, String gender,String mobilePhone) {
        User modifyUser = new User(UserContextHolder.currentUser().getUserId());
        modifyUser.setAvatar(avatar);
        modifyUser.setNickName(nickName);
        modifyUser.setGender(gender);
        modifyUser.setMobilePhone(mobilePhone);
        modifyUser.setUpdateTime(System.currentTimeMillis());
        userMapper.updateUserById(modifyUser);
        return OperationResult.newSuccessOperationResult();
    }

    @Override
    public OperationResult changePwd(String oldPwd, String newPwd) {
        if (StringUtils.isBlank(oldPwd)) {
            return OperationResult.newValidateFailOperationResult("原始密码不能为空");
        }
        if (StringUtils.isBlank(newPwd)) {
            return  OperationResult.newValidateFailOperationResult("新密码不能为空");
        }
        User loginUser = userMapper.getUserById(UserContextHolder.currentUser().getUserId());
        if (!oldPwd.equals(loginUser.getPassword())) {
            return OperationResult.newValidateFailOperationResult( "原始密码不正确");
        }
        loginUser.setPassword(newPwd);
        loginUser.setUpdateTime(System.currentTimeMillis());
        userMapper.updateUserById(loginUser);
        return OperationResult.newSuccessOperationResult();
    }

    @Override
    @Deprecated
    public List<String> getAccessStoreIds() {
        SessionUser currentSessionUser = UserContextHolder.currentUser();
        // 超管类型的用户直接返回空。这里的空可以理解为可以查看所有门店
        if ( currentSessionUser.isSuper() ) {
            return null;
        }
        // 接收该用户所属部门及子部门id
        List<String> departmentId = null;
        // 递归查询数据库
        String accessDepartmentIds = userInfoMapper.getAccessDepartmentIds(currentSessionUser.getUserId());
        if ( StringUtils.isNotBlank(accessDepartmentIds)) {
            departmentId = Arrays.asList(accessDepartmentIds.split(SymbolConstant.COMMA_EN));
            // 这里在转换成一次ArrayList .否则会在下一步remove的时候抛出java.lang.UnsupportedOperationException.
            // @see : https://blog.csdn.net/fygkchina/article/details/32329293
            departmentId = new ArrayList<>(departmentId);
            //  这里移除第1个元素，具体原因参见查询函数，由于用了字符串拼接，导致第一个元素是无效的id
            departmentId.remove(0);
        }
        if ( departmentId != null && !departmentId.isEmpty() ) {
            return departmentStoreMapper.getByDepartmentIds(departmentId);
        }
        return null;
    }

    @Override
    public List<Long> getAccessDeviceGroupIds() {
       return deviceGroupMapper.getDeviceGroupIdsByUserId(UserContextHolder.currentUser().getUserId());
    }

}
